<!--
 * createor: zc
 * email: zc8023zs@163.com
 * create_time: 2020/3/14 13:46:24
 *-->
<template>
    <el-tabs v-model="activeName" type="card" @tab-click="handleTabClick">
      <el-tab-pane label="用户标签" name="first"> 
        <div class="app-container">
            <div class="filter-container">
                <el-button class="filter-item" style="margin-left: 10px;" v-show="permissions.p_add" type="primary" icon="el-icon-edit" @click="handleCreate">
                    添加用户标签
                </el-button> 
            </div>
            <el-table
            :key="tableKey"
            :data="list"
            border
            fit
            highlight-current-row
            style="width: 100%;margin-bottom:34px;"
            >
            <el-table-column type="index" label=" " width="50" align="center" :index="table_index"></el-table-column>
            <el-table-column v-if="permissions.operation" :label="$t('table.actions')" align="center" width="230" class-name="small-padding fixed-width">
                <template slot-scope="{row}">
                <el-button type="primary" v-show="permissions.p_edit"  size="mini" @click="handleUpdate(row)">
                    {{ $t('table.edit') }}
                </el-button> 
                <el-button  size="mini" v-show="permissions.p_del"  type="danger" @click="handleDelete(row)">
                    {{ $t('table.delete') }}
                </el-button>
                </template>
            </el-table-column>
            <el-table-column label="标签名称"  align="center" min-width="200px" show-overflow-tooltip>
                <template slot-scope="{row}">
                <span>{{ row.tag_name }}</span>
                </template>
            </el-table-column>  
            </el-table>
            
            <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getList" />

          <el-dialog :title="textMap[dialogStatus]" :close-on-click-modal='false' :visible.sync="dialogFormVisible" append-to-body>
              <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" >
                  <el-form-item label="标签名称" prop="tag_name">
                  <el-input maxlength="31" v-model.trim="temp.tag_name" />
                  </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                  <el-button @click="dialogFormVisible = false">
                  {{ $t('table.cancel') }}
                  </el-button>
                  <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
                  {{ $t('table.confirm') }}
                  </el-button>
              </div>
            </el-dialog>  
        </div>
    </el-tab-pane>
    <el-tab-pane label="系统标签" name="second">
        <div class="app-container">
            <el-table
            :key="tableKeySys"
            :data="listSys"
            border
            fit
            highlight-current-row
            style="width: 100%;margin-bottom:34px;"
            >
            <el-table-column type="index" label=" " width="50" align="center" :index="table_index_sys"></el-table-column>
             <el-table-column v-if="permissions.p_sys" :label="$t('table.actions')" align="center" width="230" class-name="small-padding fixed-width">
                <template slot-scope="{row}">
                  <router-link :to="'/system/sys_tags_union?tag_class='+row.tag_name">
                    <el-button type="primary" v-show="permissions.p_sys" size="mini" style="width:120px">查看关联</el-button> 
                  </router-link>
                </template>
            </el-table-column>
            <el-table-column label="标签名称"  align="center" min-width="200px" >
                <template slot-scope="{row}">
                <span>{{ row.tag_name }}</span>
                </template>
            </el-table-column>  
            </el-table>
            
            <pagination v-show="totalSys>0" :total="totalSys" :page.sync="listQuerySys.page" :limit.sync="listQuerySys.size" @pagination="getListSys" />
 
        </div>
    </el-tab-pane> 
  </el-tabs>
</template>

<script>
import { fetchList, fetchObj, createObj, updateObj, getDeptTree, deleteObj } from '@/api/project/system/project_sys_sys_tags'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { Loading } from 'element-ui';

export default {
  name: 'a_systema_sys_tags',
  components: { Pagination },
  filters: {
  },
  data() {
    return {
      activeName: 'first',
      tableKey: 0,
      tableKeySys: 1,
      list: null,
      listSys: null,
      total: 0, 
      totalSys: 0,
      permissions:{
        p_select:false,
        p_add:false,
        p_edit: false,
        p_del: false,
        p_sys: false,
        operation: false,
      },
      listQuery: {
        tag_type:2,
        page: 1,
        size: 20
      }, 
      listQuerySys: {
        tag_type:1,
        page: 1,
        size: 20
      }, 
      temp: {
        id: 0,
        tag_name: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '添加'
      }, 
      rules: {
        tag_name: [{ required: true, message: '标签名称必填', trigger: 'change' },
          { type: "string", max: 30, message: "标签名称不超过30个字", trigger: "blur"}]
      },
    }
  },
  created() {
    this.getList(),
    this.getListSys()
    this.$per(result=>{ 
      this.permissions.p_select=result['*/system/systags/getSysTagsList']!=null;
      this.permissions.p_add=result['*/system/systags/addSysTags']!=null;
      this.permissions.p_edit=result['*/system/systags/updateSysParams']!=null;
      this.permissions.p_del=result['*/system/systags/deleteSysTag']!=null;
      this.permissions.p_sys=result['*/system/systags/getSysVideoTagsList','*/system/systags/getSysCouponTagsList']!=null;

      if(result['*/system/systags/updateSysParams']!=null || result['*/system/systags/deleteSysTag']!=null){
            this.permissions.operation=true;
      }
    })
  },
  methods: {
    table_index(index) {
      return (
        (this.listQuery.page - 1) * this.listQuery.size+
        index +
        1
      );
    },
    table_index_sys(index) {
      return (
        (this.listQuerySys.page - 1) * this.listQuerySys.size+
        index +
        1
      );
    },
    handleTabClick(tab, event){

    },
    openFullScreen() {
        const loading = this.$loading({
          lock: true,
          text: '数据处理中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.2)'
        }); 
        setTimeout(() => {
          loading.close();
        }, 2000);
    },
    getList() { 
      fetchList(this.listQuery).then(response => {
        this.list = response.dataArray.dataList;
        this.total = response.dataArray.dataCount;
      })
    },
    getListSys(){
      fetchList(this.listQuerySys).then(response => {
        this.listSys = response.dataArray.dataList;
        this.totalSys = response.dataArray.dataCount;
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {
        id: 0,
        tag_name: '',
        tag_type:2,
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.openFullScreen();
          createObj(this.temp).then(res => { 
            this.handleFilter();
            this.dialogFormVisible = false
            if (res.code == 0) {
              this.$notify({
                title: "成功",
                message: res.msg,
                type: "success",
                duration: 2000
              });
            }else{
              this.$notify({
                title: "失败",
                message: res.msg,
                type: "error",
                duration: 2000
              });
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.openFullScreen();
          const tempData = Object.assign({}, this.temp)  
          updateObj(tempData).then(res => { 
            this.handleFilter();
            this.dialogFormVisible = false;
            if (res.code == 0) {
              this.$notify({
                title: "成功",
                message: res.msg,
                type: "success",
                duration: 2000
              });
            }else{
              this.$notify({
                title: "失败",
                message: res.msg,
                type: "error",
                duration: 2000
              });
            }
          })
        }
      })
    },
    handleDelete(row) { 
      this.$confirm('确定删除该标签？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.openFullScreen();
        deleteObj(row).then(res=>{ 
          this.handleFilter();
          if (res.code == 0) {
              this.$notify({
                title: "成功",
                message: res.msg,
                type: "success",
                duration: 2000
              });
            }else{
              this.$notify({
                title: "失败",
                message: res.msg,
                type: "error",
                duration: 2000
              });
            }
      });

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    }, 
    handleUnion(){

      alert(1);
    }
  }
}
</script>